import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'Login',
      component: () => import('../views/UserLogin.vue'),
    },
    {
      path: '/home',
      name: 'HomeView',
      component: () => import('../views/UserHome.vue'),
      children: [
        {
          path: '', // 默认子路由
          redirect: 'page'
        },
        {
          path: 'page', // 移除前面的 /
          name: 'HomePage',
          component: () => import('../components/HomePage.vue'),
        },
        {
          path: 'message', // 移除前面的 /
          name: 'ApartmentMessage',
          component: () => import('../components/ApartmentMessage.vue'),
        },
        {
          path: 'properties', // 移除前面的 /
          name: 'ApartmentProperties',
          component: () => import('../components/ApartmentProperties.vue'),
        },
        {
          path: 'roomPage', // 移除前面的 /
          name: 'RoomPage',
          component: () => import('../components/RoomPageTest.vue'),
        },
        {
          path: 'userMessage', // 移除前面的 /
          name: 'UserMessage',
          component: () => import('../components/UserMessage.vue'),
        },

      ]
    },
    {
      path: '/system',
      name: 'System',
      component: () => import('../views/UserHome.vue'),
      children: [
        {
          path: '', // 默认子路由
          redirect: 'page'
        },
        {
          path: 'systemUser', // 移除前面的 /
          name: 'SystemUser',
          component: () => import('../components/SystemUser.vue'),
        },
      ]

    },
  ],
})

//路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/')
    }
  }
})


export default router

